package com.dymall.user.controller;

import com.dymall.common.domain.Result;
import com.dymall.user.domain.dto.LoginFormDTO;
import com.dymall.user.domain.vo.UserLoginVO;
import com.dymall.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/users")
public class UserController {
    private final UserService userService;

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public UserLoginVO login(@RequestBody @Validated LoginFormDTO loginFormDTO){
        //@Validated:根据 DTO 类中的约束注解来验证传入的数据是否合法。如果数据不符合预设条件，则会抛出 MethodArgumentNotValidException 异常
        return userService.login(loginFormDTO);
    }

    @ApiOperation("发送验证码接口")
    @PostMapping("/login/sendCode")
    public Result<String> sendCode(String phone){
        return userService.sendCode(phone);
    }

    @ApiOperation("短信登录接口")
    @PostMapping("/login/useCode")
    public UserLoginVO loginByPhone(String phone, String code){
        return userService.loginByPhone(phone, code);
    }

    @ApiOperation("扣减余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pw", value = "支付密码"),
            @ApiImplicitParam(name = "amount", value = "支付金额")
    })
    @PutMapping("/money/deduct")
    public void deduct(@RequestParam("pw") String password, @RequestParam("amount") Integer amount){
        userService.deduct(password, amount);
    }
}
